package cn.mfeg.servlet;

import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.mfeg.models.Project;
import cn.mfeg.services.ProjectService;
import cn.mfeg.utils.ExportExcelUtil;

public class ExportProjectServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	ApplicationContext applicationContext;
	ProjectService projectService;
	
	public void init() throws ServletException {
		applicationContext = new ClassPathXmlApplicationContext("spring-servlet.xml");
		projectService = (ProjectService) applicationContext.getBean(ProjectService.class);
	}
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Project> projects = projectService.selectAllProjectInfo();
		//创建工作空间
		HSSFWorkbook book = new HSSFWorkbook();
		// 创建一个工作表
		HSSFSheet sheet = book.createSheet("项目信息");
		// 创建第一行
		HSSFRow row = sheet.createRow(0);
		// 创建一个单元格 设置值
		HSSFCell cel = null;
		cel = row.createCell(0);
		cel.setCellValue("创建用户");
		cel = row.createCell(1);
		cel.setCellValue("项目名称");
		cel = row.createCell(2);
		cel.setCellValue("项目日期");
		cel = row.createCell(3);
		cel.setCellValue("项目状态");
		cel = row.createCell(4);
		cel.setCellValue("联系电话");
		cel = row.createCell(5);
		cel.setCellValue("主办单位");
		cel = row.createCell(6);
		cel.setCellValue("项目官网");
		cel = row.createCell(7);
		cel.setCellValue("需要专家人数");
		cel = row.createCell(8);
		cel.setCellValue("已有专家人数");
		cel = row.createCell(9);
		cel.setCellValue("优秀专家人数");
		 
		int rowIndex = 1;
		for (Project p:projects) {
			row = sheet.createRow(rowIndex++);
			cel = row.createCell(0);
			cel.setCellValue(p.getOrganiser());
			cel = row.createCell(1);
			cel.setCellValue(p.getProjectname());
			cel = row.createCell(2);
			cel.setCellValue(p.getStarttime()+"~"+p.getEndtime());
			cel = row.createCell(3);
			cel.setCellValue(p.getStatus());
			cel = row.createCell(4);
			cel.setCellValue(p.getPhonenum());
			cel = row.createCell(5);
			cel.setCellValue(p.getHostunit());
			cel = row.createCell(6);
			cel.setCellValue(p.getWebsite());
			cel = row.createCell(7);
			cel.setCellValue(p.getExperttotal());
			cel = row.createCell(8);
			cel.setCellValue("XXX");
			cel = row.createCell(9);
			cel.setCellValue("XXX");
		}
		ExportExcelUtil.exportExcel(req, resp, book);
	}
}
